/*
 * @Author: EyeSklear 1814580085@qq.com
 * @Date: 2024-01-08 10:27:01
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-05-15 15:31:17
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-03-02 16:25:12
 * @FilePath: \Scene_Back\src\picture\picture.controller.ts
 * @Description:
 *
 * Copyright (c) 2024 by EyeSklear , All Rights Reserved.
 */
/* eslint-disable prettier/prettier */
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
  Controller,
  Get,
  Res,
  Post,
  Body,
  Req,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UseGuards,
  UploadedFile,
} from '@nestjs/common';
import { PictureService } from './picture.service';
import { User } from 'src/user/entities/user.entity';
import { CreatePictureDto } from './dto/create-picture.dto';
import { UpdatePictureDto } from './dto/update-picture.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { AuthGuard } from '@nestjs/passport';
import type { Response } from 'express';
import { zip } from 'compressing';
import * as fs from 'fs';
import { request } from 'http';
import { Request } from 'express';
import { Public } from 'src/common/decorator/public.decorator';
import * as path from 'path';
@Controller('picture')
export class PictureController {
  constructor(private readonly pictureService: PictureService) {}

  @Post('singlePicture')
  @UseInterceptors(FileInterceptor('file'))
  uploadPicture(@UploadedFile() file) {
    return file.filename;
  }

  @Post('createPictureItem')
  create(@Body() createPictureDto: CreatePictureDto, @Req() request: Request) {
    const user = request.user;
    createPictureDto.creator = (user as any).email;
    return this.pictureService.create(createPictureDto);
  }

  // @Public()
  @Get('downLoadSinglePicture')
  downLoad(@Res() res: Response) {
    const url = process.env.FILE_PATH + '/image/1706258603031.png';
    res.download(url);
  }
  @Public()
  @Get('downLoadSinglePictureStream')
  async down(@Res() res: Response) {
    const url = process.env.FILE_PATH + '/image/1706258603031.png';
    const tarStream = new zip.Stream();
    await tarStream.addEntry(url);
    res.setHeader('Content-Type', 'application/octet-stream');
    res.setHeader('Content-Disposition', `attachment; filename=xiaoman`);
    tarStream.pipe(res);
  }
  @Public()
  @Get('getPictureName/:filename')
  async getFile(
    @Param('filename') filename: string,
    @Res() res: Response,
  ): Promise<void> {
    // 构建本地文件路径
    const filePath = path.resolve(process.env.FILE_PATH + '/image/' + filename);

    // 创建可读流
    const fileStream = fs.createReadStream(filePath);
    res.setHeader('Content-Type', 'image/png');
    // 设置响应头，告诉浏览器返回的是文件
    // 将文件流传输给客户端
    fileStream.pipe(res);
  }

  // @UseGuards(JwtAuthGuard)
  // @Public()
  @Get('findAll')
  async findAll(@Req() request: Request) {
    const user = request.user;
    return this.pictureService.findAll();
  }

  @Get('findOne/:id')
  async findOne(@Param('id') id: string) {
    return this.pictureService.findOne(id);
  }

  @Get('findPictureListByUser')
  async findPictureListByUser(@Req() request: Request) {
    const user = request.user;
    const email = (user as any).email;
    return this.pictureService.findPictureListByUser(email);
  }

  @Patch('updatePictureItem/:id')
  async update(
    @Param('id') id: string,
    @Body() updatePictureDto: UpdatePictureDto,
  ) {
    return this.pictureService.update(id, updatePictureDto);
  }

  @Delete('deletePictureItem/:id')
  async remove(@Param('id') id: string) {
    return this.pictureService.remove(id);
  }
}
